/*******************************发送分页查询ajax请求******************************/
var currentPage = 1; //当前页
var pageSize = 5; //每页显示条数
var totalCount = 0; //总记录条数
var totalPage = 0; //总页数

function PageFind() {
    //1.获取输入框上的值
    var account = $('#account').val();
    var tel = $('#tel').val();
    var email = $('#email').val();
    var startTime = $('#startTime').val();
    var endTime = $('#endTime').val();
    //2.发送ajax 
    $.ajax({
        url: "user/findUserByPage",
        type: "post",
        dataType: "JSON",
        data: {
            account: account,
            Tel: tel,
            email: email,
            startTime: startTime,
            endTime: endTime,
            currentPage: currentPage,
            pageSize: pageSize
        },
        success: function(pageBean) {
            //设置参数
            totalCount = pageBean.totalCount;
            totalPage = parseInt(pageBean.totalPage);
            //数据插入函数
            showTableData(pageBean.list);
        }
    })
}

//发送ajax
PageFind();

function showTableData(dataList) {
    //1.设置当前页和总页数
    var pageNumberData = currentPage + '/' + totalPage;
    $('#pageNumber').html(pageNumberData);
    //2.设置表格数据
    //2.1清空表格
    $('#tableData').html('');
    //2.2遍历数据
    var trs = '';
    for (var i = 0; i < dataList.length; i++) {
        var dataDetail = dataList[i];
        //判断修改数据
        if (dataDetail.userState == 1) {
            dataDetail.userState = '启用';
        } else if (dataDetail.userState == 0) {
            dataDetail.userState = '禁用';
        }
        if (dataDetail.userSex == 1) {
            dataDetail.userSex = '男';
        } else if (dataDetail.userSex == 0) {
            dataDetail.userSex = '女';
        }
        //改日期
        var cDate = new Date(dataDetail.userCtime).format('yyyy-MM-dd hh:mm:ss');
        var tr = `
               <tr>
                    <td>${dataDetail.userName}</td>
                    <td>${dataDetail.userNiname}</td>
                    <td>${dataDetail.userSex}</td>
                    <td>${dataDetail.userTel}</td>
                    <td>${dataDetail.userEmail}</td>
                    <td>${cDate}</td>
                    <td>${dataDetail.userState}</td>
                    <td>
                        <button class="btn btn-mini " type="button" id='reSetPwd'>重置密码</button>
                        <button class="btn btn-mini " type="button" id='banPickBtn'>禁用/启用</button>
                    </td>
                </tr>`;
        //添加数据
        trs += tr;
    }
    //2.3将数据加入
    $('#tableData').html(trs);
    //3.设置禁用启用按钮的事件
    $('[id = banPickBtn]').click(function() {
        var userName = $(this).parent().parent().find('td').eq(0).html();
        var userState = $(this).parent().parent().find('td').eq(6).html();
        banAjax(userName, userState);
    });
    //4.重置密码事件
    $('[id = reSetPwd]').click(function() {
        //设置账号
        $('#updateAccount').val($(this).parent().parent().find('td').eq(0).html());
        //清空隐藏
        $('#olePwd').val('');
        $('#newPwd').val('');
        $('#rePwd').val('');
        //显示弹窗
        showPanel();
    })
}
//上一页 下一页按钮 搜索 重置 触发事件
$('#prePageBtn').click(function() {
    currentPage -= 1;
    if (currentPage < 1) {
        currentPage = 1;
        return;
    }
    PageFind();
});
$('#nextPageBtn').click(function() {
    currentPage += 1;
    if (currentPage > totalPage) {
        currentPage = totalPage;
        return;
    }
    PageFind();
});
$('#searchBtn').click(function() {
    PageFind();
});
$('#reSetBtn').click(function() {
    $('#account').val('');
    $('#tel').val('');
    $('#email').val('');
    $('#startTime').val('');
    $('#endTime').val('');
});
/* *****************禁用启用按钮事件********************* */
function banAjax(userName, userState) {
    $.ajax({
        url: "user/banOrEnable",
        type: "post",
        dataType: "JSON",
        data: {
            userName: userName,
            userState: userState
        },
        success: function(data) {
            layer.confirm('状态修改成功', {
                btn: ['确定'] //按钮
            });
            PageFind();
        }
    })
};
/* **********************发送修改密码AJAX*********************** */
function updatePwdAjax() {
    //1.获取参数
    var account = $('#updateAccount').val();
    var olePwd = $('#olePwd').val();
    var newPwd = $('#newPwd').val();
    var rePwd = $('#rePwd').val();
    //2.判断
    if (olePwd == '' || newPwd == '' || rePwd == '') {
        layer.confirm('密码框中不能为空，请重新输入', {
            btn: ['确定'] //按钮
        });
        return;
    }
    if (newPwd != rePwd) {
        layer.confirm('2次新输入的密码不相同，请重新输入', {
            btn: ['确定'] //按钮
        });
        return;
    }
    //3.发送AJAX
    $.ajax({
        url: "user/alterPwd",
        type: "post",
        dataType: "JSON",
        data: {
            account: account,
            olePwd: olePwd,
            newPwd: newPwd
        },
        success: function(data) {
            if (data.id == 1) {
                //清空隐藏
                $('#olePwd').val('');
                $('#newPwd').val('');
                $('#rePwd').val('');
                hide();
                //修改成功
                layer.confirm(data.message, {
                    btn: ['确定'] //按钮
                });
            } else if (data.id == 0) {
                //修改失败
                layer.confirm(data.message, {
                    btn: ['确定'] //按钮
                });
            }
        }
    })
};